#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
UWSGI=../sbin/uwsgi
UWSG_CONF=../config/uwsgi.ini

currTime=$(date "+%Y_%m_%d")

if [ ! -n "$1" ]; then
    echo "Usages: sh uwsgiC.sh [start|stop]"
    exit 0
fi

if [ ! -d ../logs ] ;then
        mkdir ../logs
    fi

if [ "$1" = start ] ;then
    psid=$(ps -ef |grep "./uwsgi"|grep -v grep| wc -l)
    echo ${psid}
    if [ ${psid} -gt 4 ] ;then
        echo "uwsgi is running!"
        exit 0
    else
        nohup  ${UWSGI} ${UWSG_CONF} >> ../logs/${currTime}_uwsgi.log 2>&1 &
        sleep 3
        if [[ -s ../logs/uwsgi.pid ]];then  
            echo "Start uwsgi service [OK]"
        else
           echo "Start uwsgi service [Fail],exit"
           exit
        fi
    fi
elif [ "$1" = stop ] ;then
    ${UWSGI} --stop  ../logs/uwsgi.pid
    if [ $? -eq 0 ];then
        echo "Stop uwsig service [OK]"
    else
        echo "Stop uwsig service [Fail],exit"
        exit
    fi
else
    echo "Usages: sh uwsgiC.sh [start|stop]"
fi

